/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <windows.h>
#include <commctrl.h>

#include "DamDialog.h"
#include "Track.h"
#include "TrackInfoControl.h"

TrackInfoControl::TrackInfoControl(DamDialog *parent, unsigned int bottom) throw(): Control(parent, 0, bottom - 30, parent->width(), 30), m_hNameLabel(0), m_hName(0), m_hVolumeLabel(0), m_hVolume(0), m_hPanLabel(0), m_hPan(0) {
    m_hNameLabel = CreateWindow(TEXT("STATIC"), TEXT("Name:"), WS_CHILD | WS_VISIBLE | SS_LEFT | SS_NOPREFIX, Dialog::widthToPixels(4), Dialog::heightToPixels(4), Dialog::widthToPixels(20), Dialog::heightToPixels(8), hWnd(), 0, parent->hInstance(), 0);
    m_hName = CreateWindow(TEXT("EDIT"), 0, WS_CHILD | WS_DISABLED | WS_BORDER | WS_VISIBLE | ES_LEFT, Dialog::widthToPixels(28), Dialog::heightToPixels(2), Dialog::widthToPixels(width() - 32), Dialog::heightToPixels(12), hWnd(), 0, parent->hInstance(), 0);
    m_hVolumeLabel = CreateWindow(TEXT("STATIC"), TEXT("Vol:"), WS_CHILD | WS_VISIBLE | SS_LEFT | SS_NOPREFIX, Dialog::widthToPixels(4), Dialog::heightToPixels(18), Dialog::widthToPixels(20), Dialog::heightToPixels(8), hWnd(), 0, parent->hInstance(), 0);
    m_hVolume = CreateWindow(TEXT("EDIT"), 0, WS_BORDER | WS_CHILD | WS_DISABLED | WS_VISIBLE | ES_LEFT | ES_NUMBER, Dialog::widthToPixels(28), Dialog::heightToPixels(16), Dialog::widthToPixels(24), Dialog::heightToPixels(12), hWnd(), 0, parent->hInstance(), 0);
    m_hPanLabel = CreateWindow(TEXT("STATIC"), TEXT("Pan:"), WS_CHILD | WS_VISIBLE | SS_LEFT | SS_NOPREFIX, Dialog::widthToPixels(parent->width() / 2), Dialog::heightToPixels(18), Dialog::widthToPixels(20), Dialog::heightToPixels(8), hWnd(), 0, parent->hInstance(), 0);
    m_hPan = CreateWindow(TEXT("EDIT"), 0, WS_BORDER | WS_CHILD | WS_DISABLED | WS_VISIBLE | ES_LEFT, Dialog::widthToPixels(parent->width() / 2 + 28), Dialog::heightToPixels(16), Dialog::widthToPixels(24), Dialog::heightToPixels(12), hWnd(), 0, parent->hInstance(), 0);
}

TrackInfoControl::~TrackInfoControl() {
    DestroyWindow(m_hPan);
    DestroyWindow(m_hPanLabel);
    DestroyWindow(m_hVolume);
    DestroyWindow(m_hVolumeLabel);
    DestroyWindow(m_hName);
    DestroyWindow(m_hNameLabel);
}

void TrackInfoControl::setCurrentTrack(Track *currTrack) throw() {
    m_currTrack = currTrack;

    if(m_currTrack == 0) {
        EnableWindow(m_hName, FALSE);
        EnableWindow(m_hVolume, FALSE);
        EnableWindow(m_hPan, FALSE);
        SendMessage(m_hName, WM_SETTEXT, 0, 0);
        SendMessage(m_hVolume, WM_SETTEXT, 0, 0);
        SendMessage(m_hPan, WM_SETTEXT, 0, 0);
    }
    else {
        EnableWindow(m_hName, TRUE);
        EnableWindow(m_hVolume, TRUE);
        EnableWindow(m_hPan, TRUE);
        SendMessage(m_hName, WM_SETTEXT, 0, LPARAM(m_currTrack->name()));
        TCHAR volBuf[128], panBuf[128];
        wsprintf(volBuf, TEXT("%d"), m_currTrack->volume());
        wsprintf(panBuf, TEXT("%d"), m_currTrack->pan());
        SendMessage(m_hVolume, WM_SETTEXT, 0, LPARAM(volBuf));
        SendMessage(m_hPan, WM_SETTEXT, 0, LPARAM(panBuf));
    }
}

void TrackInfoControl::paint(HDC hDc) const throw() {
}

void TrackInfoControl::controlNotification(HWND hCtrl, WPARAM notification) throw() {
    if(notification == EN_CHANGE && m_currTrack != 0) {
        if(hCtrl == m_hName) {
            TCHAR name[2048];
            SendMessage(hCtrl, WM_GETTEXT, 1024, LPARAM(name));
            m_currTrack->setName(name);
        }
        else if(hCtrl == m_hVolume) {
            TCHAR volBuf[128];
            SendMessage(hCtrl, WM_GETTEXT, 128, LPARAM(volBuf));
            unsigned long vol = _tcstoul(volBuf, 0, 10);
            m_currTrack->setVolume(vol > 255 ? 255 : static_cast<unsigned char>(vol));
        }
        else if(hCtrl == m_hPan) {
            TCHAR panBuf[128];
            SendMessage(hCtrl, WM_GETTEXT, 128, LPARAM(panBuf));
            long pan = _tcstol(panBuf, 0, 10);
            if(pan < -128) {
                pan = -128;
            }
            else if(pan > 127) {
                pan = 127;
            }
            m_currTrack->setPan(static_cast<char>(pan));
        }
    }
}

void TrackInfoControl::click(unsigned int x, unsigned int y) throw() {
}
